//
//  PhotoBrowserAnimated.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/19.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit

protocol PhotoBrowserPresentDelegate: NSObjectProtocol {
    //需要展现的图片
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    // 缩略图原来位置
    func photoBrowserFromRect(indexPath: NSIndexPath) -> CGRect
    // 大图的位置
    func photoBrowserToRect(indexPath: NSIndexPath) -> CGRect
}

protocol PhotoBrowserDismissDelegate: NSObjectProtocol {
    func imageViewForDismiss() -> UIImageView
    func indexPathForDismiss() -> NSIndexPath
}

class PhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {
    //定义是present还是dismiss的标记
    private var isPresented = false
    ///展现动画的代理
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    ///解除动画代理
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    ///展现动画对应索引
    var indexPath: NSIndexPath?
    
    ///设置代理属性
    func setDelegateParams(presentDelegate: PhotoBrowserPresentDelegate, dismissDelegate: PhotoBrowserDismissDelegate, indexPath: NSIndexPath) {
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    
    //指定负责动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
}

extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {
    //动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1
    }
    //具体动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? presentAnimation(transitionContext): dismissAnimation(transitionContext)

    }
    //展现动画
    func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        guard let presentDelegate = presentDelegate, indexPath = indexPath else {
            return
        }
        //1.真正要展现的视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        toView.alpha = 0
        
        transitionContext.containerView()?.addSubview(toView)
        //2.获取“大米”视图
        let iv = presentDelegate.imageViewForPresent(indexPath)
        iv.frame = presentDelegate.photoBrowserFromRect(indexPath)
        transitionContext.containerView()?.addSubview(iv)
        
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            iv.frame = presentDelegate.photoBrowserToRect(indexPath)
            toView.alpha = 1
            iv.alpha = 0
            
            }) { (_) -> Void in
                
                iv.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    //dismiss动画
    func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        guard let presentDelegate = presentDelegate, dismissDelegate = dismissDelegate else {
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        transitionContext.containerView()?.addSubview(fromView)
        //解除动画中的“大米”视图
        let iv = dismissDelegate.imageViewForDismiss()
        let indexPath = dismissDelegate.indexPathForDismiss()
        transitionContext.containerView()?.addSubview(iv)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromView.alpha = 0
            iv.frame = presentDelegate.photoBrowserFromRect(indexPath)
            }) { (_) -> Void in
                fromView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
}